package com.hr.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hr.entity.AoaInMailList;
import com.hr.entity.AoaMailnumber;
import com.hr.entity.AoaUser;
import com.hr.service.impl.AoaMailnumberServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.logging.Logger;

/**
 * (AoaMailnumber)邮箱控制层
 *
 *
 * @author makejava
 * @since 2022-06-16 11:44:24
 */
@RestController
@RequestMapping("/aoaMailnumber")
public class AoaMailnumberController {
    /**
     * 服务对象
     */
    @Autowired
    private AoaMailnumberServiceImpl aoaMailnumberService;
    //日志
    Logger logger = Logger.getLogger("info");
    /**
     * 分页查询
     * @param current 当前页码
     * @return 查询结果
     */
    @GetMapping("/getMailInfo")
    public Page queryByPage(Integer current,String name) {
        Page<AoaMailnumber> aoaMailnumbers;
        if(name!=""&&name!=null&&name.trim()!=""){
            aoaMailnumbers = aoaMailnumberService.queryByPage(current,name);
            return aoaMailnumbers;
        }else{
            aoaMailnumbers = aoaMailnumberService.queryByPage(current,null);
            return aoaMailnumbers;
        }
    }
    /**
     * 查询单条数据
     */
    @GetMapping("/getMailOne")
    public AoaMailnumber getMailOne(String id){
        return aoaMailnumberService.queryOne((long) Integer.parseInt(id));
    }
    /**
     * 提交修改数据
     * @return
     */
    @PostMapping("/saveEdit")
    public ResponseEntity editCommit(@RequestBody @Validated String mailList){
        return aoaMailnumberService.editMailnumber(JSON.parseObject(mailList, AoaMailnumber.class));
    }
    /**
     * 删除数据
     */
    @DeleteMapping("/deleteData")
    public ResponseEntity deleteData(String id){
        return aoaMailnumberService.deleteData(id);

    }
    /**
     *添加保存数据
     */
    @PostMapping("/saveaccount")
    public ResponseEntity saveData(AoaMailnumber aoaMailnumber){
        aoaMailnumber.setMailCreateTime(new Date());
        return ResponseEntity.ok(aoaMailnumberService.saveData(aoaMailnumber));
    }

    @Autowired
    com.hr.config.ShiroConfig shiroConfig;

    /**
     * 根据登入用户id查询内部邮箱消息
     * @param current 当前页码
     * @return
     */
    @GetMapping("/queryMailList")
    public Page<AoaInMailList> getMailList(Integer current){
        ThreadContext.bind(shiroConfig.defaultWebSecurityManager());
        Subject subject = SecurityUtils.getSubject();
        PrincipalCollection principals = subject.getPrincipals();
        AoaUser aoaUser = (AoaUser) principals.getPrimaryPrincipal();
        Long userId = aoaUser.getUserId();
        return aoaMailnumberService.getMailList(userId,current);
    }
    /**
     * 根据id列表删除数据
     */
    @DeleteMapping("/deleteMail")
    public ResponseEntity delMail(Integer[] arr){
        return aoaMailnumberService.delMail(arr);
    }
    /**
     * 標記已讀
     */
    @PostMapping("/asterisk")
    public ResponseEntity asterisk(Integer[] values){
        return aoaMailnumberService.asterisk(values);
    }
}

